<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class news_category extends CI_Controller
{
	protected $_templates;
	
	function __construct() 
	{
		parent::__construct();
		$this->pre_message = "";         
		$this->permit_library->check_permit();
		$this->session->set_userdata(array('Url'=>uri_string()));
		//load language
		$this->load->helper('search') ;
		$this->lang->load('news_category');
		$this->load->library('string_library');
		$this->load->model('news_category_model','news_category');
	}   
	
	function index()
	{
		/*Begin search*/
		$type = $this->input->post('type');
		$data['search_bar']= searchBar(array(),array('cat_name'=>'Category Name'),'news_category/index/');
		if($type=='ajax')
		{
			$arr_search['key_search'] = $this->input->post('key_search');
			$arr_search['field_search'] = $this->input->post('field_search');
			$this->session->set_userdata('key_search',$arr_search['key_search']);
			$this->session->set_userdata('field_search',$arr_search['field_search']);
		}
		else
		{
			$arr_search['key_search']=$this->session->userdata('key_search');
			$arr_search['field_search']=$this->session->userdata('field_search');
		}
		/*End  search*/
		
		$data['title'] = 'Danh sách chuyên mục tin tức';
		$data['add'] = 'news_category/add';
		$config['base_url'] = base_url().'news_category/index/'.(int)$this->uri->segment(3);  
		$data['num'] = $this->news_category->get_num_data($arr_search);
		$config['total_rows'] =  $data['num'];
		$config['per_page'] = 20;
		$config['uri_segment'] = 4;   
		$this->pagination->initialize($config);   
		
		$data['list'] =   $this->news_category->get_all_data($config['per_page'],(int)$this->uri->segment(4),$arr_search);
		$data['pagination']    = $this->pagination->create_links();            
		$this->_templates['page'] = 'news_category/showlist';
	    if($type=="ajax")
			echo $this->load->view($this->_templates['page'],$data,true);
		else
			$this->site->load($this->_templates['page'],$data); 
	}
	
	function add()
	{                         
		$data['title'] = 'Thêm mới chuyên mục tin tức';
		$data['option_pid'] = $this->news_category->optionCategoryLevel();
		
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('cat_name',lang('news_category_name'),'trim|required');
			if($this->form_validation->run() == FALSE){
				$this->pre_message = validation_errors();
			}else{
				$data = $this->build_data();
				if($this->news_category->save($data))
				{
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('news_category/index');
				}
				else
				{
					$this->pre_message = 'Lưu không thành công';
				}
			}
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'news_category/add';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function edit($id)
	{                         
		$data['title'] = 'Sửa chuyên mục tin tức';
		$data['rs'] = $this->news_category->get_detail($id);
		$data['option_pid'] = $this->news_category->optionCategoryLevel(0,$data['rs']->parent_id,4);
		
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('cat_name',lang('news_category_name'),'trim|required');
			//$this->form_validation->set_rules('name_block',lang('news_category_name_en'),'trim|required');
			if($this->form_validation->run() == FALSE){
				$this->pre_message = validation_errors();
			}else{
				 $data = $this->build_data(1);
				 if($this->news_category->save($data, $id)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('news_category/index');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				 }
			}
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'news_category/edit';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function del()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		
		if($this->news_category->deleted($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
			redirect('news_category/index/'.$page);
	}
	
	function dels()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
		
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->news_category->deleted($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('news_category/index/'.$page);
	}
	
	function build_data($flag=1)
	{
		$req["parent_id"] = $this->input->post("parent_id");
		$req["cat_name"] = $this->input->post("cat_name");
		$req["name_block"] = $this->input->post("name_block");
		$req["name_alias"] = $this->input->post("name_alias");
		$req["nb_order"] = $this->input->post("nb_order");
		$req["is_menu"] = (int) $this->input->post("is_menu");
		$req["bl_active"] = (int) $this->input->post("bl_active");
		
		$req["nb_level"] = $req["parent_id"]? 2 : 1 ; //$this->input->post("nb_level");
		return $req;
	}
	
	function creatmennu()
	{
		$str = "<?php  \n/**\n* Cache file for news.dev \n* Date: ".date('d/m/y H:i:s').".\n**/";
		
		$listcat_cap1 = $this->news_category->getCategory(0);
		$c1=1;
		$total_menu =count($listcat_cap1);
		$str .= "\n";
		$str .= "\n\$menu['total_menu'] = $total_menu;"; 
		$str .= "\n";  
		// menu cap 1          
		foreach($listcat_cap1 as $cap1):
			$str .="/*********Cấp 1_".$c1."**************/";
			$cap1_id = $cap1->cat_id;
			$cap1_name = str_replace("'","\'",$cap1->cat_name);
			$cap1_alias = str_replace("'","\'",$cap1->name_alias);
			
			$str .= "\n\$menu['menu_id_leve_1_$c1'] = $cap1_id;";   
			$str .= "\n\$menu['menu_name_leve_1_$c1'] = '$cap1_name';";   
			$str .= "\n\$menu['menu_alias_leve_1_$c1'] = '$cap1_alias';";   
			
			$listcat_cap2 = $this->news_category->getCategory($cap1->cat_id);
			$total_cap2 = count($listcat_cap2);
			$str .= "\n\$menu['menu_total_leve_1_$c1'] = $total_cap2;";
			
			if($total_cap2 > 0)
			{
				$c2 = 1;
				foreach($listcat_cap2 as $cap2):
					$str .= "\n";
					$str .="\n/*********Cấp 2_".$c1."_".$c2."**************/";
					$cap2_id = $cap2->cat_id;
					$cap2_name = str_replace("'","\'",$cap2->cat_name);
					$cap2_alias = str_replace("'","\'",$cap2->name_alias);
					
					$str .= "\n\$menu['menu_id_leve_2_".$c1."_".$c2."'] = $cap2_id;";   
					$str .= "\n\$menu['menu_name_leve_2_".$c1."_".$c2."'] = '$cap2_name';"; 
					$str .= "\n\$menu['menu_alias_leve_2_".$c1."_".$c2."'] = '$cap2_alias';"; 
					   
					$listcat_cap3 = $this->news_category->getCategory($cap2->cat_id);
					$total_cap3 = count($listcat_cap3);
					$str .= "\n\$menu['menu_total_leve_2_".$c1."_".$c2."'] = $total_cap3;";
					if($total_cap3 > 0)
					{
						$c3 = 1;
						foreach($listcat_cap3 as $cap3):
							$str .= "\n";
							$str .="\n/*********Cấp 3_".$c1."_".$c2."_".$c3."**************/";                            
							$cap3_id = $cap3->cat_id;
							$cap3_name = str_replace("'","\'",$cap3->cat_name);
							$cap3_alias = str_replace("'","\'",$cap3->name_alias);
							
							$str .= "\n\$menu['menu_id_leve_3_".$c1."_".$c2."_".$c3."'] = $cap3_id;";   
							$str .= "\n\$menu['menu_name_leve_3_".$c1."_".$c2."_".$c3."'] = '$cap3_name';";    
							$str .= "\n\$menu['menu_alias_leve_3_".$c1."_".$c2."_".$c3."'] = '$cap3_alias';";    
							
							$listcat_cap4 = $this->news_category->getCategory($cap3->cat_id);
							$total_cap4 = count($listcat_cap4);
							$str .= "\n\$menu['menu_total_leve_3_".$c1."_".$c2."_".$c3."'] = $total_cap4;";
							if($total_cap4 > 0)
							{
								$c4 = 1;
								foreach($listcat_cap4 as $cap4):
									$str .= "\n";
									$str .="\n/*********Cấp 4_".$c1."_".$c2."_".$c3."_".$c4."**************/";                            
									$cap4_id = $cap4->cat_id;
									$cap4_name = str_replace("'","\'",$cap4->cat_name);
									$cap4_alias = str_replace("'","\'",$cap4->name_alias);
									
									$str .= "\n\$menu['menu_id_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = $cap4_id;";   
									$str .= "\n\$menu['menu_name_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = '$cap4_name';";    
									$str .= "\n\$menu['menu_alias_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = '$cap4_alias';";    
									
								$c4++;                   
								endforeach;  
							}   
							$str .= "\n";
							
						$c3++;                   
						endforeach;  
					}   
					$str .= "\n";                     
				$c2++;                   
				endforeach;  
			}
			$str .= "\n";
			$str .= "\n";
		$c1++;
		endforeach;
		$str .= "\n";
		$str .= "\n";
		$str .= "\n\n/* End of file menu*/\n/* Location: ./data/menu.php */";   
		// thuc hien ghi file
		$dir=ROOT.'/data/menu.php';	
		return file_put_contents($dir,$str);
	}
}
?>
